﻿<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <script>
        /*
          解题思路：
              1. 分析需求;
              2. 编写代码;
              3. 调试代码;
        */
          // 将数组  ['关羽', '张飞', '程咬金','吕布']  转换为字符串
        var arr = ['关羽', '张飞', '程咬金', '吕布'];

        let result = "[";
        for (let index in arr) {
            // console.log(index, arr[index]);
            // result = result + "\'" + arr[index] + "\'" + ", ";
            // 最后一个元素单独处理
            if (index == arr.length - 1) {
                // console.log(arr[index] + "是最后一个元素");
                result = result + "\'" + arr[index] + "\'";
            } else {
                result = result + "\'" + arr[index] + "\'" + ", ";
            }
        }
        result = result + "]";
        console.log(result);
    </script>
</body>
</html>